//
//  A_2ViewController.m
//  RACExamale
//
//  Created by cym_bj on 2017/11/29.
//  Copyright © 2017年 com.cym. All rights reserved.
//

#import "A_2ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface A_2ViewController ()
@property (nonatomic,strong) id<RACSubscriber>subscriber;
@end

@implementation A_2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"取消订阅（RACDisposable）";
    
    
    
    /*
     
     1、默认一个信号发送数据完毕就会主动取消订阅
     
     2、只要订阅者在，就不会自动取消信号订阅，需要手动去取消  ： dispose
     
     */
    
    
    //自动取消
    [self examale1];
    
    //手动取消
    [self examale2];
   
    
    
}


#pragma mark - 自动取消
-(void)examale1{
    
    RACSignal *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        
        [subscriber sendNext:@"发送信号"];
        
        return [RACDisposable disposableWithBlock:^{
            
            //只要信号取消订阅就会调用
            //清空资源
            NSLog(@"信号被取消");
        }];
    }];
    
    
    [signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@",x);
    }];
    
}

#pragma mark- 手动取消
-(void)examale2{
    
    RACSignal *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        
        self.subscriber=subscriber;
        
        [subscriber sendNext:@"发送数据"];
        
        return [RACDisposable disposableWithBlock:^{
            
            NSLog(@"被取消");
            
            
        }];
    }];
    
    
    RACDisposable *disposable =[signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
        
    }];
    
    //取消订阅信号
    [disposable dispose];
    
}


@end
